/**匹配界面 */
class MatchingInterface extends eui.Component {
	public constructor(gameType: number) {
		super();
		st_config.setScreenAuto(this);
		this.gameType = gameType;
		//数据初始提前
		this.dataInit();
		this.skinName = "resource/assets/skins/matchMain.exml";
		//显示初始在后
		this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
	}
	/**离开 */
	private leave: eui.Button;
	/**说明 */
	private explanation: eui.Button;
	/**用户名字 */
	private userName: eui.Label;
	/**用户历史最高分 */
	private userMaxBefor: eui.Label;
	/**用户今日最高分 */
	private userMaxNow: eui.Label;
	/**今日第一 */
	private nowFirst: eui.Label;
	/**等待人数 */
	private waitNum: eui.Label;
	/**游戏模式
	 * 1-单机
	 * 2-联网
	 */
	private gameType: number;
	/**等待人数 */
	private waitManNum: number;
	/**单人模式定时器 */
	private singleModeTimer: egret.Timer;
	/**游戏循环定时器 */
	private gameLoopTimer: egret.Timer;
	/**说明关闭按钮 */
	private close: eui.Button;
	/**说明界面 */
	private explanationMain: eui.Group;
	/**说明介绍 */
	private caption: eui.Label;
	/**用户头像 */
	private userHeadShow: eui.Image;
	/**数据初始化 */
	public dataInit() {
		this.waitManNum = 1;
		this.gameLoopTimer = new egret.Timer(1000, 0);
		if (this.gameType == 1) {
			this.singleModeTimer = new egret.Timer(400, serverConfig.config.maxUserCountByRoom);
		} else if (this.gameType == 2) {

		}
	}
	/**显示初始化 */
	public init() {

		this.explanationMain.visible = false;
		this.leave.addEventListener(egret.TouchEvent.TOUCH_TAP, this.leaveFun, this);
		this.explanation.addEventListener(egret.TouchEvent.TOUCH_TAP, this.explanationFun, this);
		this.close.addEventListener(egret.TouchEvent.TOUCH_TAP, e => { this.explanationMain.visible = false }, this);
		this.userMsgShow();
		this.todayFirst();
		this.waitNumShow();
		if (this.gameType == 1) {
			this.singleModeTimer.addEventListener(egret.TimerEvent.TIMER, this.singMatchFun, this);
			this.singleModeTimer.start();
		} else if (this.gameType == 2) {

		}
		this.gameLoopTimer.addEventListener(egret.TimerEvent.TIMER, this.waitNumShow, this);
		this.gameLoopTimer.start();
		//platform.creatBanner();
	}
	/**单机匹配 */
	public singMatchFun() {
		if (this.waitManNum >= serverConfig.config.maxUserCountByRoom) {
			this.waitNumShow();
			this.singleModeTimer.stop();
			this.singleModeTimer.removeEventListener(egret.TimerEvent.TIMER, this.singMatchFun, this);
			this.gameLoopTimer.stop();
			this.gameLoopTimer.removeEventListener(egret.TimerEvent.TIMER, this.waitNumShow, this);
			//UIManager.getInstance().openFirstUI(UIManager.CLASS_UI_INDEX_STORY, TweenManager.TWEEN_UI_MOVE);//(2,1)
			SceneManager.instance.SitchScene(SceneName.单机场景);
			// if (this.parent) {
			// 	this.parent.removeChild(this);
			// }
		}
		this.waitManNum++;
	}
	/**离开 */
	public leaveFun() {
		if (this.gameType == 1) {
			this.singleModeTimer.stop();
			this.singleModeTimer.removeEventListener(egret.TimerEvent.TIMER, this.singMatchFun, this);
			this.gameLoopTimer.stop();
			this.gameLoopTimer.removeEventListener(egret.TimerEvent.TIMER, this.waitNumShow, this);
			SceneManager.instance.SitchScene(SceneName.登录场景);
			if (this.parent) {
				this.parent.removeChild(this);
			}
		} else if (this.gameType == 2) {

		}
	}
	/**说明 */
	public explanationFun() {
		if (this.gameType == 1) {
			this.caption.text = "暂时没想好说什么,总之就是3分钟谁分最多谁赢!!!";
		} else if (this.gameType == 2) {
			this.caption.text = "还是没想好怎么说,就是和密友一起合作抢分吧!!!";
		}
		this.explanationMain.visible = true;
	}
	/**用户信息显示 */
	public userMsgShow() {
		this.userName.text = gameGlobal.global.playerName;
		if (DataProl.maxLong == 0) {
			this.userMaxBefor.text = "历史最高 :  ---";
		} else {
			this.userMaxBefor.text = "历史最高 :  " + DataProl.maxLong;
		}
		if (DataProl.todayUserMaxGrade <= 0) {
			this.userMaxNow.text = "今日最高 :  ---";
		} else {
			this.userMaxNow.text = "今日最高 :  " + DataProl.todayUserMaxGrade;
		}
		if (weixinManager.getInstance().getUserInfo().avatarUrl) {
			this.userHeadShow.source = weixinManager.getInstance().getUserInfo().avatarUrl;
		}
	}
	/**今日第一 */
	public todayFirst() {
		if (DataProl.todayMaxGrade <= 0) {
			this.nowFirst.text = "暂无";
		} else {
			this.nowFirst.text = "当前今日第一 : " + DataProl.todayMaxName + " " + DataProl.todayMaxGrade + "分";
		}
	}
	/**等待人数显示 */
	public waitNumShow() {
		this.waitNum.text = "等待中人数 : " + this.waitManNum + "人（满" + serverConfig.config.maxUserCountByRoom + "人开赛)";
	}

}